<?php
namespace app\modules\api\models;

use yii;
use yii\db\Query;

class Invoice_new
{
    private $table;

    public function __construct()
    {
        $this->table = yii::$app->db->tablePrefix.'invoice_new';
    }

    public function list($p, $s, $where = [])
    {
        $db = new Query();
        $db->select('*')->from($this->table);
        if ($where['job_number']) {
            $db->andWhere(['job_numer'=>$where['job_number']]);
        }
        if ($where['invoice']) {
            $db->andWhere(['invoice'=>$where['invoice']]);
        }
        if ($where['invoice_status']) {
            $db->andWhere(['invoice_status'=>$where['invoice_status']]);
        }
        $db->andWhere(['!=','invoice','']);
        if ($where['begin_date']) {
            $db->andWhere(['>=','date',$where['begin_date']]);
        }
        if ($where['end_date']) {
            $db->andWhere(['<=','date',$where['end_date']]);
        }
        return $db->orderBy(['date'=>SORT_DESC,'id'=>SORT_DESC])->offset(($p-1)*$s)->limit($s)->all();
    }

    /**
     * 获取数据条数
     * @param array $where
     * @return int 
     */
    public function size($where = []):int 
    {
        $db = new Query();
        $db = new Query();
        $db->select('count(*) as num')->from($this->table);
        if ($where['job_number']) {
            $db->andWhere(['job_numer'=>$where['job_number']]);
        }
        if ($where['invoice']) {
            $db->andWhere(['invoice'=>$where['invoice']]);
        }
        if ($where['invoice_status']) {
            $db->andWhere(['invoice_status'=>$where['invoice_status']]);
        }
        $db->andWhere(['!=','invoice','']);
        if ($where['begin_date']) {
            $db->andWhere(['>=','date',$where['begin_date']]);
        }
        if ($where['end_date']) {
            $db->andWhere(['<=','date',$where['end_date']]);
        }

        $re = $db->one();
        return $re?$re['num']:0;
    }
}
